import {numToChn} from 'util'
import {calendar} from 'calendar'

//同时支持阳历和阴历的日期对象
class Date2 {
  static LunarDayArr = ['', '初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', 
                        '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十',
                        '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十']

  constructor (is_lunar = false, year = 2020, month = 1, day = 1, is_leap_month = false) {
    this.is_lunar = is_lunar  //是否是阴历
    this.year = parseInt(year)
    this.month = parseInt(month)
    this.day = parseInt(day)
    this.is_leap_month = is_leap_month  //如果是阴历的情况下，月份是否是闰月
  }   

  //解析数据库的日期字符串： 阳历格式  2020-01-01；  阴历格式  2020-001-01 其中月份第一个0表示当月是否是闰月。
  ParseDbDateStr(date_str) {
    if (date_str.length == 10) {
      //阳历
      let timestamp = Date.parse(date_str)
      if (isNaN(timestamp)) {
          return false
      }

      let d = new Date(timestamp)
      this.is_lunar = false
      this.year = d.getFullYear()
      this.month = d.getMonth() + 1
      this.day = d.getDate()
      this.is_leap_month = false
      return true

    } else if (date_str.length == 11) {
      //阴历
      let regArr = /^(\d{4})-(\d{3})-(\d{2})$/g.exec(date_str)
      if (regArr) {
        //匹配成功, 返回格式如： ["1234-010-10", "1234", "010", "10"]
        let year = parseInt(regArr[1])
        let is_leap_month = regArr[2][0] == "1"
        let month = parseInt(regArr[2].substr(1))
        let day = parseInt(regArr[3])
        if (year < 1900 || year > 2100) {
          return false   //农历库只支持1900到2100
        }
        if (month < 1 || month > 12) {
          return false
        }

        //校验闰月
        if (is_leap_month) {
          let leap_month = calendar.leapMonth(year)  //计算当年实际闰月，没有闰月取0
          if (leap_month == 0) {
            return false 
          } else {
            if (leap_month != month) {
              return false
            }
          }
        } 

        //校验天数
        let month_days_num = 0  //本月总天数
        if (is_leap_month) {
          month_days_num = calendar.leapDays(year)
        } else {
          month_days_num = calendar.monthDays(year, month)
        }
        if (day < 1 || day > month_days_num) {
          return false
        }

        this.is_lunar = true
        this.year = year
        this.month = month
        this.day = day
        this.is_leap_month = is_leap_month
        return true

      } else {
        //匹配失败
        return false
      }
    } else {
      return false
    }
  }

  //生成数据库格式的日期字符串
  FormatDbDateStr() {
    if (this.is_lunar) {
      //农历
      let year = ('' + this.year).padStart(4, '0')
      let leap = this.is_leap_month ? '1' : '0'
      let month = ('' + this.month).padStart(2, '0')
      let day = ('' + this.day).padStart(2, '0')
      return year + '-' + leap + month + '-' + day
    } else {
      let year = ('' + this.year).padStart(4, '0')
      let month = ('' + this.month).padStart(2, '0')
      let day = ('' + this.day).padStart(2, '0')
      return year + '-' + month + '-' + day
    }
  }

  //返回用于展示的标准字符串:  2020年1月1日 阳历 ； 一九八六年闰六月初一 阴历
  FormatShowDate() {
    if (this.is_lunar) {
      //阴历
      // let year = numToChn(this.year)
      // let month = numToChn(this.month)
      // let day = Date2.LunarDayArr[this.day]
      let year = this.year
      let month = this.month
      let day = this.day
      let leap = this.is_leap_month ? '闰' : ''

      return year + '年' + leap + month + '月' + day + ' 阴历'

    } else {
      //阳历
      return this.year + '年' + this.month + '月' + this.day + '日 阳历'
    }
  }

  //公历转农历, 传入公历年月日整数。 返回农历的Date2对象，失败返回null
  static Solar2Lunar(y, m, d) {
    let result = calendar.solar2lunar(y, m, d)
    if (typeof(result) != "object") {
      return null
    }
    return new Date2(true, result.lYear, result.lMonth, result.lDay, result.isLeap)
  }

  //返回今天的农历对象
  static GetLunarToday() {
    let timestamp = new Date().getTime()
    let d = new Date(timestamp)
    return Date2.Solar2Lunar(d.getFullYear(), d.getMonth() + 1, d.getDate())
  }

  //返回今天的阳历对象
  static GetSolarToday() {
    let timestamp = new Date().getTime()
    let d = new Date(timestamp)
    return new Date2(false, d.getFullYear(), d.getMonth() + 1, d.getDate(), false)
  }
}

module.exports = {
  Date2
}